package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Constantitem;
import com.foreknow.service.ConstantitemService;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/constantitem")
public class ConstantitemController {
    @Autowired
    private ConstantitemService constantitemService;

    @RequestMapping(value = "/findConstantitemByPage",method = RequestMethod.POST)
    @ResponseBody
    public String findConstantitemByPage(@RequestBody Map<String,Object> param) throws JsonProcessingException {
        //设置分页
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        String query = (String) param.get("query");
        List<Constantitem> list = constantitemService.searchAllConstantitem(query);
        HashMap<String,Object> hs = new HashMap<>();
        if (list != null){
            PageInfo<Constantitem> info = new PageInfo<>(list);
            hs.put("data",info);
            hs.put("meta",new Constance("获取常数列表成功",200));
        }else{
            hs.put("meta",new Constance("获取常数列表失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addConstantitem",method = RequestMethod.POST)
    @ResponseBody
    public String insertConstantitemInfo(@RequestBody Constantitem constantitem) throws JsonProcessingException {
        int x = constantitemService.insertConstantitem(constantitem);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("添加常数项成功", 200));
        } else {
            hs.put("meta", new Constance("添加常数项失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteConstantitem",method = RequestMethod.GET)
    @ResponseBody
    public String deleteConstantitemInfo(@RequestParam Integer id) throws JsonProcessingException {
        int x = constantitemService.deleteConstantitemById(id);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("删除常数项成功", 200));
        } else {
            hs.put("meta", new Constance("删除常数项失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectConstantitem",method = RequestMethod.GET)
    @ResponseBody
    public String selectConstantitemForUpdate(@RequestParam Integer id) throws JsonProcessingException {
        Constantitem constantitem = constantitemService.searchConstantitemById(id);
        HashMap<String,Object> hs = new HashMap<>();
        if (constantitem != null){
            hs.put("data",constantitem);
            hs.put("meta",new Constance("获取常数项成功",200));
        }else{
            hs.put("meta",new Constance("获取常数项失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updatetConstantitem",method = RequestMethod.POST)
    @ResponseBody
    public String updateConstantitemFromSelect(@RequestBody Constantitem constantitem) throws JsonProcessingException {
        int x = constantitemService.updateConstantitemByObject(constantitem);
        HashMap<String, Object> hs = new HashMap<>();
        if (x == 1) {
            hs.put("meta", new Constance("修改常数项成功", 200));
        } else {
            hs.put("meta", new Constance("修改常数项失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "findConstanttypeitemByPage",method = RequestMethod.POST)
    @ResponseBody
    public String findConstanttypeitemByPage(@RequestBody Map<String,Object> param) throws JsonProcessingException {
        int pagenum = (int) param.get("pagenum");
        int pagesize = (int) param.get("pagesize");
        PageHelper.startPage(pagenum,pagesize);
        Integer id = (Integer) param.get("id");
        List<Constantitem> list = constantitemService.searchAllConstantTypeItem(id);
        HashMap<String,Object> hs = new HashMap<>();
        if (list != null){
            PageInfo<Constantitem> info = new PageInfo<>(list);
            hs.put("data",info);
            hs.put("meta",new Constance("获取常数类别详情成功",200));
        }else {
            hs.put("meta",new Constance("获取常数类别详情失败",201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}
